package com.agileai.bpm.controller.instance;

import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.bpm.bizmoduler.define.WfProcessManage;
import com.agileai.bpm.domain.running.WorkItem;
import com.agileai.bpm.service.running.ProcessHandle;
import com.agileai.bpm.service.running.ProcessHandle.UserSeqType;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.BaseHandler;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;

public class WfProcessInstanceViewHandler extends BaseHandler{
	public ViewRenderer prepareDisplay(DataParam param){
		this.setAttribute("WFP_ID",param.get("WFP_ID"));
		this.setAttribute("WFIP_ID",param.get("WFIP_ID"));
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
	
	@PageAction
	public ViewRenderer retrieveJsonData(DataParam param){
		String responseText = FAIL;
		WfProcessManage manage = this.getService();
		DataRow row = manage.getRecord(param);
		String graphData = row.getString("WFP_GRAPH");
		try {
			JSONObject restore = new JSONObject(graphData);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("restore", restore);

			String processInstanceId = param.get("WFIP_ID");
			List<DataRow> records = getService().findRunningActivityList(processInstanceId);
			JSONObject activeRects = new JSONObject();
			JSONArray rectArray = new JSONArray();
			
			if (records != null && records.size() > 0){
				for (int i=0;i < records.size();i++){
					DataRow tempRow = records.get(i);
					JSONObject tempRect = new JSONObject();
					tempRect.put("paths",new JSONArray());
					tempRect.put("code",tempRow.getString("WFA_CODE"));
					rectArray.put(tempRect);					
				}
			}
			
			JSONObject rects = new JSONObject();
			rects.put("rects", rectArray);
			activeRects.put("activeRects", rects);
			
			jsonObject.put("activeRects", activeRects);
			
			responseText = jsonObject.toString();
			
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer getTaskStatJson(DataParam param){
		String responseText = "";
		String userCode = param.get("userCode");
		int maxLimit = param.getInt("maxLimit", 5);
		JSONArray jsonArray = new JSONArray();
		
		try {
			JSONObject todoTaskListJSONObject = new JSONObject();
			this.processTodoTaskList(todoTaskListJSONObject,userCode,maxLimit);
			jsonArray.put(todoTaskListJSONObject);

			JSONObject doingTaskListJSONObject = new JSONObject();
			this.processDoingTaskList(doingTaskListJSONObject,userCode,maxLimit);
			jsonArray.put(doingTaskListJSONObject);
			
			JSONObject didTaskListJSONObject = new JSONObject();
			this.processDidTaskList(didTaskListJSONObject,userCode,maxLimit);
			jsonArray.put(didTaskListJSONObject);
			
			responseText = jsonArray.toString();
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
		return new AjaxRenderer(responseText);
	}
	
	private void processTodoTaskList(JSONObject todoTaskListJSONObject,String userCode,int maxLimit) throws JSONException{
		todoTaskListJSONObject.put("tabTitle", "我的待办");
		todoTaskListJSONObject.put("tabCode", "myTodo");
		todoTaskListJSONObject.put("moreURL", "javascript:doRedirectPortalPage('targetPage:03/ToDoTaskList.ptml')");
		todoTaskListJSONObject.put("showHeader", true);
		
		JSONArray propertyConfigs = new JSONArray();
		todoTaskListJSONObject.put("propertyConfigs",propertyConfigs);
		
		JSONObject codeConfigJsonObject = new JSONObject();
		codeConfigJsonObject.put("code", "taskTitle");
		codeConfigJsonObject.put("name", "任务名称");
		codeConfigJsonObject.put("isLink", true);
		codeConfigJsonObject.put("isLinkURL", false);
		propertyConfigs.put(codeConfigJsonObject);
		
		JSONObject graphTextConfigJsonObject = new JSONObject();
		graphTextConfigJsonObject.put("code", "graphText");
		graphTextConfigJsonObject.put("name", "流程图");
		graphTextConfigJsonObject.put("isLink", true);
		graphTextConfigJsonObject.put("isLinkURL", false);
		propertyConfigs.put(graphTextConfigJsonObject);
		
		JSONObject linkConfigJsonObject = new JSONObject();
		linkConfigJsonObject.put("code", "taskTitleLink");
		linkConfigJsonObject.put("name", "任务链接");
		linkConfigJsonObject.put("isLink", false);
		linkConfigJsonObject.put("isLinkURL", true);
		propertyConfigs.put(linkConfigJsonObject);
		
		JSONObject graphlinkConfigJsonObject = new JSONObject();
		graphlinkConfigJsonObject.put("code", "graphTextLink");
		graphlinkConfigJsonObject.put("name", "流程图链接");
		graphlinkConfigJsonObject.put("isLink", false);
		graphlinkConfigJsonObject.put("isLinkURL", true);
		propertyConfigs.put(graphlinkConfigJsonObject);
		
		JSONArray datas = new JSONArray();
		todoTaskListJSONObject.put("datas", datas);
		
		ProcessHandle processHandle = (ProcessHandle)this.lookupService("processHandle");
		try {
			List<WorkItem> workItems = processHandle.findWorkItemList(userCode, ProcessHandle.UserSeqType.USER_CODE);
			if (workItems != null && workItems.size() > 0){
				int count = workItems.size() > maxLimit ? maxLimit:workItems.size();
				for (int i = 0;i < count;i++){
					WorkItem workItem = workItems.get(i);
					JSONObject jsonObject = new JSONObject();	
					jsonObject.put("taskTitle", workItem.getTitle());
					jsonObject.put("graphText", "查看");
					
					String processId = workItem.getProcessId();
					String processInstId = workItem.getProcInstanceId();
					
					jsonObject.put("taskTitleLink", "javascript:viewTaskDetail('"+workItem.getBusinessURL()+"')");
					jsonObject.put("graphTextLink", "javascript:viewInstanceGraphBox('"+processInstId+"','"+processId+"')");
					datas.put(jsonObject);
				}
			}
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(), e);
		}
	}
	
	private void processDoingTaskList(JSONObject doingTaskListJSONObject,String userCode,int maxLimit)  throws JSONException{
		doingTaskListJSONObject.put("tabTitle", "我的在办");
		doingTaskListJSONObject.put("tabCode", "myDoing");
		doingTaskListJSONObject.put("moreURL", "javascript:doRedirectPortalPage('targetPage:03/OnDoTaskList.ptml')");
		doingTaskListJSONObject.put("showHeader", true);
		
		JSONArray propertyConfigs = new JSONArray();
		doingTaskListJSONObject.put("propertyConfigs",propertyConfigs);
		
		JSONObject codeConfigJsonObject = new JSONObject();
		codeConfigJsonObject.put("code", "taskTitle");
		codeConfigJsonObject.put("name", "任务名称");
		codeConfigJsonObject.put("isLink", false);
		codeConfigJsonObject.put("isLinkURL", false);
		propertyConfigs.put(codeConfigJsonObject);
		
		JSONObject graphTextConfigJsonObject = new JSONObject();
		graphTextConfigJsonObject.put("code", "graphText");
		graphTextConfigJsonObject.put("name", "流程图");
		graphTextConfigJsonObject.put("isLink", true);
		graphTextConfigJsonObject.put("isLinkURL", false);
		propertyConfigs.put(graphTextConfigJsonObject);
		
		JSONObject graphlinkConfigJsonObject = new JSONObject();
		graphlinkConfigJsonObject.put("code", "graphTextLink");
		graphlinkConfigJsonObject.put("name", "流程图链接");
		graphlinkConfigJsonObject.put("isLink", false);
		graphlinkConfigJsonObject.put("isLinkURL", true);
		propertyConfigs.put(graphlinkConfigJsonObject);
		
		JSONArray datas = new JSONArray();
		doingTaskListJSONObject.put("datas", datas);
		
		WfProcessManage wfProcessManage = getService();
		List<DataRow> records = wfProcessManage.queryDoingWorkList(userCode, UserSeqType.USER_CODE);
		if (records != null){
			int count = records.size() > maxLimit ? maxLimit:records.size();
			for (int i = 0;i < count;i++){
				DataRow row = records.get(i);
				JSONObject jsonObject = new JSONObject();	
				jsonObject.put("taskTitle", row.getString("WFIP_TITLE"));
				jsonObject.put("graphText", "查看");
				
				String processId = row.getString("WFP_ID");
				String processInstId = row.getString("WFIP_ID");
				
				jsonObject.put("graphTextLink", "javascript:viewInstanceGraphBox('"+processInstId+"','"+processId+"')");
				datas.put(jsonObject);
			}
		}
	}
	
	private void processDidTaskList(JSONObject didTaskListJSONObject,String userCode,int maxLimit)  throws JSONException{
		didTaskListJSONObject.put("tabTitle", "我的已办");
		didTaskListJSONObject.put("tabCode", "myDid");
		didTaskListJSONObject.put("moreURL", "javascript:doRedirectPortalPage('targetPage:03/DidTaskList.ptml')");
		didTaskListJSONObject.put("showHeader", true);
		
		JSONArray propertyConfigs = new JSONArray();
		didTaskListJSONObject.put("propertyConfigs",propertyConfigs);
		
		JSONObject codeConfigJsonObject = new JSONObject();
		codeConfigJsonObject.put("code", "taskTitle");
		codeConfigJsonObject.put("name", "任务名称");
		codeConfigJsonObject.put("isLink", false);
		codeConfigJsonObject.put("isLinkURL", false);
		propertyConfigs.put(codeConfigJsonObject);
		
		JSONObject graphTextConfigJsonObject = new JSONObject();
		graphTextConfigJsonObject.put("code", "graphText");
		graphTextConfigJsonObject.put("name", "流程图");
		graphTextConfigJsonObject.put("isLink", true);
		graphTextConfigJsonObject.put("isLinkURL", false);
		propertyConfigs.put(graphTextConfigJsonObject);
		
		JSONObject graphlinkConfigJsonObject = new JSONObject();
		graphlinkConfigJsonObject.put("code", "graphTextLink");
		graphlinkConfigJsonObject.put("name", "流程图链接");
		graphlinkConfigJsonObject.put("isLink", false);
		graphlinkConfigJsonObject.put("isLinkURL", true);
		propertyConfigs.put(graphlinkConfigJsonObject);
		
		
		JSONArray datas = new JSONArray();
		didTaskListJSONObject.put("datas", datas);
		
		WfProcessManage wfProcessManage = getService();
		List<DataRow> records = wfProcessManage.queryDidWorkList(userCode, UserSeqType.USER_CODE);
		if (records != null){
			int count = records.size() > maxLimit ? maxLimit:records.size();
			for (int i = 0;i < count;i++){
				DataRow row = records.get(i);
				JSONObject jsonObject = new JSONObject();	
				
				jsonObject.put("taskTitle", row.getString("WFHP_TITLE"));
				jsonObject.put("graphText", "查看");
				
				String processId = row.getString("WF_ID");
				String processInstId = row.getString("WFHP_ID");
				
				jsonObject.put("graphTextLink", "javascript:viewHistoryGraphBox('"+processInstId+"','"+processId+"')");
				
				datas.put(jsonObject);
			}
		}
	}
	
	protected WfProcessManage getService() {
		return (WfProcessManage)this.lookupService(WfProcessManage.class);
	}
}
